<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

class base_DA {

    /**
     * (PHP 5)<br/>
     * Create a db connexion.
     * @return PDO
     */
    private function create_db_connexion(){
        $xml = simplexml_load_file('config.xml');

        $db_connexion = new PDO('mysql:host='.$xml->database->dbserver.';dbname='.$xml->database->dbname, $xml->database->dbuser, $xml->database->dbpassword, array(1002 => 'SET NAMES utf8'));
        return $db_connexion;
    }

    /**
     * (PHP 5)<br/>
     * Get db table prefix
     * @return string
     */
    protected function get_table_prefix(){
        $xml = simplexml_load_file('config.xml');
        return $xml->database->table_prefix;
    }

    /**
     * (PHP 5)<br/>
     * Execute an sql command
     * @param string $sql_cmd
     * @param bool $is_insert
     * @return array
     */
    protected function execute_sql_command($sql_cmd, $is_insert){
        try {
            $db = $this->create_db_connexion();
            $cmd = $db->prepare($sql_cmd);

//            echo var_dump($sql_cmd).'<br/><br/>';

            $cmd->execute();

            if ((!isset($is_insert)) || (!$is_insert)){
                $result = $cmd->fetchAll();
            }
            else {
                $result = $db->lastInsertId();
            }
        }
        catch (Exception $ex){
            echo 'Exception : ',  $ex->getMessage(), "\n";
        }

        if (isset($db)) { unset($db); }

        return $result;
    }

    /**
     * (PHP 5)<br/>
     * Format a string value parameter for an sql command
     * @param string $string
     * @return string
     */
    protected function format_string_to_sql_param($string) {
        return '"'.$string.'"';
    }

    /**
     * (PHP 5)<br/>
     * Format a date or datetime value parameter for an sql command
     * @param date $date
     * @return string
     */
    protected function format_date_to_sql_param($date) {
        return '\''.$date.'\'';
    }

    /**
     * (PHP 5)<br/>
     * Return 'null' string if $value is null
     * @param string $value
     * @return string
     */
    protected function format_null_value($value){
        return (isset($value) ? $value : 'null');
    }
}
?>
